import i18n from '@/i18n'
import dayjs from 'dayjs'

export function getTimeTextByDuration(duration: number, options?: { ceilToMinute?: boolean; ceilZero?: boolean }) {
  if (typeof duration !== 'number' || duration === 0) return ''

  let second = duration

  // 向上取整分钟数
  if (options?.ceilToMinute && duration % 60 !== 0) {
    const fillSecond = 60 - (duration % 60)
    second += fillSecond
  }

  // 0分钟向上取整
  if (options?.ceilZero && second < 60) {
    second = 60
  }

  const formattedTime = dayjs().hour(0).minute(0).second(second).format('H:m')
  const [hour, minute] = formattedTime.split(':')

  const timeTexts = []

  if (hour !== '0') {
    const hourText = i18n.global.t('APP.Common.TimeUnit.Hour.Text').replace('{hour}', hour)
    timeTexts.push(hourText)
  }

  // 分钟不为0 或 时钟为0且分钟为0 显示分钟
  if (minute !== '0' || (hour === '0' && minute === '0')) {
    const minuteText = i18n.global.t('APP.Common.TimeUnit.Minute.Text').replace('{minute}', minute)
    timeTexts.push(minuteText)
  }

  return timeTexts.join(' ')
}
